/**
 * Definition for a binary tree node.
 * function TreeNode(val, left, right) {
 *     this.val = (val===undefined ? 0 : val)
 *     this.left = (left===undefined ? null : left)
 *     this.right = (right===undefined ? null : right)
 * }
 */
/**
 * @param {TreeNode} root
 * @return {number[]}
 */
 var largestValues = function(root) {
  let res = []
  let queue = []
  if(root === null){
    return res
  }
  queue.push(root)
  while(queue.length){
    let curLever = -Infinity
    let len = queue.length
    for(let i = 0;i < len;i++){
      let node = queue.shift()
      curLever = node.val > curLever ? node.val :curLever
      if(node.left) queue.push(node.left)
      if(node.right) queue.push(node.right)
    }
    res.push(curLever)
  }
  return res
};